package com.cgtong.android.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.adapter.AdviseAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.AdviseBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.mvp.contract.GetAdviseContract;
import com.cgtong.android.mvp.presenter.GetAdvisePresenter;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.RecycleViewDivider;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kingja.loadsir.callback.SuccessCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的建议
 */
public class MyAdviseActivity extends BaseTitleActivity implements GetAdviseContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.title_right_txt)
    TextView mTitleRightTxt;

    private List<AdviseBean> mList = new ArrayList<>();
    private BaseQuickAdapter<AdviseBean, BaseViewHolder> mAdapter;

    private GetAdvisePresenter getAdvisePresenter;

    @Override
    protected String getContentTitle() {
        return getString(R.string.txt_me_advise);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_advise;
    }

    @Override
    protected void loadDataByNet() {
        initView();
        getAdvisePresenter = new GetAdvisePresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    private void initView() {
        mTitleRightTxt.setVisibility(View.VISIBLE);
        mTitleRightTxt.setText(getString(R.string.title_right_feedback));
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mList.clear();
                        initData();
                        mAdapter.notifyDataSetChanged();
                        mSwipeLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AdviseAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, R.drawable.shape_line));
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                String content = mAdapter.getItem(position).getContent();
                String time = mAdapter.getItem(position).getAskDate();
                String replytime =  mAdapter.getItem(position).getReplyDate();
                String replyContent =  mAdapter.getItem(position).getReply();
                MyAdviseDetailActivity.start2Activity(MyAdviseActivity.this, content, time,replytime,replyContent);
            }
        });
    }

    private void initData() {
        getAdvisePresenter.getAdviseList();
    }

    @OnClick({R.id.title_right_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_right_txt:
                startActivity(new Intent(MyAdviseActivity.this, FeedbackActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void setDatas(List<AdviseBean> list) {
        mList.clear();
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
        mAdapter.addData(list);
    }

    @Override
    public void setNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }
}
